/**
 * MybatisConfiguration.java 2016年11月15日 11:19
 * <p>
 * Copyright (c) 2010-2016 yinghezhong Inc. All rights reserved.
 *
 * @Description
 * @version 1.0
 */
package com.ql.cloud.framework.config.dao.config.mybatis;

import com.ql.cloud.framework.config.dao.config.datasource.ReadWriteRoutingDataSource;
import com.ql.cloud.framework.config.dao.config.datasource.TransactionManagerConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
/**
 * MyBatis配置
 *
 * @author 齐龙
 * @date 2016年11月15日 11:19
 *
 */
@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean({MybatisProperties.class, ReadWriteRoutingDataSource.class})
@AutoConfigureAfter(TransactionManagerConfiguration.class)
public class MybatisConfiguration {

    @Autowired
    private ConfigurableApplicationContext applicationContext;


    private Logger log = LoggerFactory.getLogger(getClass());

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactories() throws Exception {
        MybatisProperties mybatisProperties = applicationContext.getBean(MybatisProperties.class);
        log.info("sqlSessionFactory init");
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        ReadWriteRoutingDataSource dataSource = (ReadWriteRoutingDataSource) applicationContext.getBean("readWriteRoutingDataSource");
        bean.setDataSource(dataSource);

        // 分页插件
        /*
         * PageHelper pageHelper = new PageHelper(); Properties properties = new Properties();
         * properties.setProperty("reasonable", "true");
         * properties.setProperty("supportMethodsArguments", "true");
         * properties.setProperty("returnPageInfo", "check"); properties.setProperty("params",
         * "count=countSql"); pageHelper.setProperties(properties);
         * 
         * //添加插件 bean.setPlugins(new Interceptor[]{pageHelper});
         */

        // 添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setConfigLocation(resolver.getResource(mybatisProperties.getConfigLocation()));
            Resource[] resource = resolver.getResources(mybatisProperties.getMapperLocations());
            bean.setMapperLocations(resource);
        } catch (Exception e) {
            log.error("未找到MyBatis SQLMapper文件", e);
        }
        return bean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate() {
        SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext.getBean("sqlSessionFactory");
        return new SqlSessionTemplate(sessionFactory);
    }

//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//    }
}
